{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os \n",
    "import re \n",
    "import json \n",
    "import numpy as np \n",
    "import pandas as pd "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>index</th>\n",
       "      <th>display_name</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mid</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>/m/09x0r</th>\n",
       "      <td>0</td>\n",
       "      <td>Speech</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>/m/05zppz</th>\n",
       "      <td>1</td>\n",
       "      <td>Male speech, man speaking</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>/m/02zsn</th>\n",
       "      <td>2</td>\n",
       "      <td>Female speech, woman speaking</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>/m/0ytgt</th>\n",
       "      <td>3</td>\n",
       "      <td>Child speech, kid speaking</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>/m/01h8n0</th>\n",
       "      <td>4</td>\n",
       "      <td>Conversation</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "           index                   display_name\n",
       "mid                                            \n",
       "/m/09x0r       0                         Speech\n",
       "/m/05zppz      1      Male speech, man speaking\n",
       "/m/02zsn       2  Female speech, woman speaking\n",
       "/m/0ytgt       3     Child speech, kid speaking\n",
       "/m/01h8n0      4                   Conversation"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "class_labels_indices = pd.read_csv(\"class_labels_indices.csv\", index_col=1)\n",
    "class_labels_indices.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "mid2tag = dict(class_labels_indices['display_name'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "645"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mid2events = json.load(open(\"audioset_id2events_expand.json\"))\n",
    "len(mid2events)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "count    527.000000\n",
       "mean      10.607211\n",
       "std        8.879602\n",
       "min        1.000000\n",
       "25%        5.000000\n",
       "50%        9.000000\n",
       "75%       14.000000\n",
       "max       70.000000\n",
       "dtype: float64"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tag2more_names = {v:[v.lower()] for v in mid2tag.values()}\n",
    "for mid, events in mid2events.items():\n",
    "    if \"~\" in mid:\n",
    "        mid = mid[:mid.rfind(\"~\")]\n",
    "    if mid in mid2tag:\n",
    "        tag2more_names[mid2tag[mid]].extend(events)\n",
    "name_stats = pd.Series(len(v) for v in tag2more_names.values())\n",
    "name_stats.describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open(\"tag2more_names.json\", \"w\") as f:\n",
    "    json.dump(tag2more_names, f, ensure_ascii=False, indent=4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "74d07dc07065639914d79fc95a011a49c7b91ce6bf0d82680fb06e81dd1e87b0"
  },
  "kernelspec": {
   "display_name": "Python 3.6.10 64-bit ('py36': conda)",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.10"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
